﻿// 设置权限
var setAuthorize;
// 设置表单数据
var setFormData;
// 验证数据是否填写完整
var validForm;
var validBusinessData;
// 保存数据
var save;

var isUpdate = false;

var bootstrap = function ($, learun) {
    "use strict";

    var fileInfoId = '';
    var userinfo;

    // 设置权限
    setAuthorize = function (data, isLook) {
        userinfo = learun.clientdata.get(['userinfo']);
        $("#createUsername").val(userinfo.user.realName);
        $('#folderId').lrselect({
            url: config.base_server() + 'filefolders/tree?access_token=' + config.getToken().access_token,
            type: 'tree',
            allowSearch: false,
            height: 225
        });

    };
    // 设置表单数据
    setFormData = function (processId, wfType) {
        if (processId) {
            $('#files').lrUploader({isView: false, value: processId});

            $.lrSetForm(config.base_server() + 'fileinfos/findByProcessId?processId=' + processId + "&fileInfoId=" + '', function (data) {
                console.log(data);
                if (data != null) {
                    isUpdate = true;
                    $('#form').lrSetFormData(data);
                }
                setPbtn(wfType);
            });
        } else {
            setPbtn(wfType);
        }
    };
    // 验证数据是否填写完整
    validForm = function (code) {
        if (!$('#form').lrValidform()) {
            return false;
        }
        if (code == 'create') {
            if ($('#files').find('.lrUploader-input').text() == '') {
                learun.alert.error("请上传【附件】");
                return false;
            }
        }

        return true;
    };
    validBusinessData = function () {
        return true;
    }
    // 保存调用函数
    save = function (processId, force, callBack, i) {
        var keyValue = "";

        if (isUpdate) {
            keyValue = processId;
        }

        var formData = $('#form').lrGetFormData(fileInfoId);

        var postData = {
            id: processId,
            files: formData.files,
            name: formData.name,
            folderId: formData.folderId || ''
        };


        $.lrSaveForm(config.base_server() + 'fileinfos/saveOrUpdate?keyValue=' + keyValue + '&force=' + force + '&access_token=' + config.getToken().access_token, JSON.stringify(postData), function (res) {
            // 保存成功后才回调
            if (!!callBack) {
                callBack(res, i);
            }
        });
    };


    // 设置父级窗口的按钮
    function setPbtn(wfType) {
        if (!!wfType && wfType == 100) {
           // $('#lr-layout-panel-right', window.parent.document).hide();

        } else {
            $('#release', window.parent.document).text('提交文档审核');
            // $('.lr-form-item-title', window.parent.document).text('备注');
            $('#savedraft', window.parent.document).hide();
            $('#sign', window.parent.document).hide();
            $('#flow-title input', window.parent.document).val("文件发布");
            $('#flow-title input', window.parent.document).attr("readonly", "readonly");
            //$('#print', window.parent.document).hide();
        }
    }
}